package com.wen.es;

import com.wen.es.annotation.EsTypeAnno;
import com.wen.es.domain.ChildType;
import com.wen.es.domain.ParentType;
import com.wen.es.service.EsService;
import com.wen.es.service.entity.EsDataWrap;
import com.wen.es.service.impl.EsServiceImpl;
import com.wen.es.utils.DataGenerater;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * es主类
 * Created by LAIO on 2018/6/30.
 */
public class EsMain {


    public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException {
        EsMain esMain = new EsMain();
        esMain.uploadTestData();
    }

    public void uploadTestData() throws IOException, InstantiationException, IllegalAccessException {
        EsService esService = new EsServiceImpl();
        String index = "test_index";
        String parType = ParentType.class.getAnnotation(EsTypeAnno.class).propertyName();
        String chiType = ChildType.class.getAnnotation(EsTypeAnno.class).propertyName();
//        esService.deleteIndex(index);
        esService.craeteIndex(index);
//        {"cur_key":2101222}
//        {"cur_key":2101293}
//        {"cur_key":2101340}
//        {"cur_key":2101352}
//        {"cur_key":2101409}
        int curKey = 1693973;
        int maxNum = 1000000000;
        int uploadNum = 50;
        long startTime = System.currentTimeMillis(), endTime = startTime + maxNum;
        long startKey = curKey;
        long sleepTime = 0;
        List<EsDataWrap> parData = new LinkedList<>();
        List<EsDataWrap> chiData = new LinkedList<>();
        ParentType par;
        List<ChildType> childList;
        int i;
        for (i = 0; i < maxNum; i++) {
            par = DataGenerater.generateClass(ParentType.class);
            childList = DataGenerater.generateClassList(ChildType.class);
            par.setKey(String.valueOf(curKey++));
            parData.add(new EsDataWrap(par.getKey(), null, par));
            for (ChildType child : childList) {
                child.setKey(String.valueOf(curKey++));
                chiData.add(new EsDataWrap(child.getKey(), par.getKey(), child));
            }
            if (parData.size() > uploadNum) {
                upload(esService, index, parType, parData);
//                    esService.uploadData(index, parType, parData);
                parData.clear();
                System.out.println("{\"cur_key\":" + curKey + "}");
            }
            if (chiData.size() > uploadNum) {
                upload(esService, index, chiType, chiData);
//                esService.uploadData(index, chiType, chiData);
                chiData.clear();
                System.out.println("{\"cur_key\":" + curKey + "}");
            }
            endTime = System.currentTimeMillis();

            if (endTime - startTime > 5000) {
                System.out.println("{spend:" + ((double) (endTime - startTime) / (curKey - startKey)) + "ms/t}");
                sleepTime = (curKey - startKey) * 5 - (endTime - startTime);
                if (sleepTime > 0) {
                    try {
                        System.out.println("{\"cur_key\":" + curKey);
                        System.out.println("\"sleep\":\"" + sleepTime + "毫秒\"}");
                        Thread.sleep((curKey - startKey) * 5 - (endTime - startTime));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                startTime = System.currentTimeMillis();
                startKey = curKey;
            }


        }
    }

    public void upload(EsService esService, String index, String chiType, List<EsDataWrap> parData) {
        while (true) {
            try {

                esService.uploadData(index, chiType, parData);
                break;
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("上传失败 睡眠1000ms");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }


}
